package com.gaoxk.interceptor;

import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

// 在消息读取阶段（READ）拦截，确保输入流用 UTF-8 解码
public class ClientUtf8InInterceptor extends AbstractPhaseInterceptor<Message> {
    public ClientUtf8InInterceptor() {
        super(Phase.READ);
    }

    @Override
    public void handleMessage(Message message) throws Fault {
        // 获取原始输入流，用 UTF-8 重新包装
        InputStream inputStream = message.getContent(InputStream.class);
        if (inputStream != null) {
            // 强制用 UTF-8 解码输入流
            message.setContent(InputStream.class,
                    new InputStreamReader(inputStream, StandardCharsets.UTF_8));
        }
    }
}
